package com.heima.article.listen;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.PublishArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author mrchen
 * @date 2022/7/8 11:33
 */
@Component
@Slf4j
public class PublishArticleListener {
    @Autowired
    private ApArticleService apArticleService;
    @RabbitListener(queuesToDeclare = @Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE))
    public void handlePublishArticleMsg(String newsId){
        log.info("接收到定时发布文章消息  文章id:{}   当前时间:{}",newsId, LocalDateTime.now().toString());
        try {
            apArticleService.publishArticle(Integer.valueOf(newsId));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("处理文章发布失败，原因:  {}",e.getMessage());
        }
    }
}
